﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class administrator_TheNap : System.Web.UI.Page
{
    dulieuDataContext db = new dulieuDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BT_Search_Click(object sender, EventArgs e)
    {
        int iMenhGia = Convert.ToInt32(DDL_MenhGia.SelectedValue);
        string sTrangThai = DDL_Status.SelectedValue;
        List<card> olist = null;
        switch (sTrangThai)
        {
            case "ALL":
                if (iMenhGia>0)
                    olist = db.cards.Where(a => a.VND == iMenhGia).ToList();
                else
                    olist = db.cards.ToList();
                break;
            case "NOT":
                olist = db.cards.Where(a => a.VND == iMenhGia && a.UserId==0).ToList();
                break;
            case "OK":
                olist = db.cards.Where(a => a.VND == iMenhGia && a.UserId!=0).ToList();
                break;
        }
        GridView1.PageIndex = 0;
        GridView1.DataSource = olist;
        GridView1.DataBind();
        Session["ListCard"] = olist;
    }
    protected void BT_Create_Click(object sender, EventArgs e)
    {
        int iMenhGia = Convert.ToInt32(DDL_MenhGiaCreate.SelectedValue);
        int iSoLuong = Convert.ToInt32(DDL_SoLuong.SelectedValue);

        for (int i = 0; i < iSoLuong; i++)
        {
            string skey = MarkeRandom();
            card oCard = new card
                             {
                                 VND = iMenhGia,
                                 UserId = 0,
                                 datecreate = DateTime.Now,
                                 code = skey
                             };
            db.cards.InsertOnSubmit(oCard);
            System.Threading.Thread.Sleep(100);
        }
        db.SubmitChanges();
        MessageBox.Show("Tạo thẻ thành công!");
    }

    private static string MarkeRandom()
    {
        string s = "";
        Random oRandom = new Random();
        for (int i = 0; i < 15; i++)
        {
            s += oRandom.Next(0, 9).ToString();
        }
        return s;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        List<card> olist =(List<card>) Session["ListCard"];
        GridView1.DataSource = olist;
        GridView1.DataBind();
    }
}